﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class MeshCtor2 : MonoBehaviour {

    [System.Serializable]
    public class Model
    {
        public float[] factor = new float[5];

        public delegate void SetFactorSignal(int index, float initFactor, float curFactor);

        public SetFactorSignal setFactorSignal;

        public void SetFactor(int index, float curFactor)
        {
            float initFactor = factor[index];
            factor[index] = curFactor;
            if (setFactorSignal != null) setFactorSignal(index, initFactor, curFactor);
        }
    }

    public Model model = null;

    public Transform[] pivot;
    //public Transform[] pivotArrow;
    public Transform center;



    void Start()
    {
        //set out-let setting
        List<Transform> pivot = new List<Transform>();
        //List<Transform> arrow = new List<Transform>();
        foreach (Transform child in transform)
        {
            if (child.name.StartsWith("center")) center = child;
            if (child.name.StartsWith("pivot")) pivot.Add(child);
            //if (child.name.StartsWith("arrow")) arrow.Add(child);
        }

        this.pivot = pivot.OrderBy(t => int.Parse(t.name.Split("_"[0])[1])).ToArray();
        //this.pivotArrow = arrow.OrderBy(t => int.Parse(t.name.Split("_"[0])[1])).ToArray();


        ///set Model
        this.model = new Model();
        this.model.setFactorSignal += On_SetFactorSignal;

        for (int i = 0; i < 5; i++)
        {
            this.model.SetFactor(i, 0.5f);
        }

        ///set View
        UpdateGeometryV2();
    }

    #region Controller
    void OnGUI()
    {
        for (int i = 0; i < 5; i++)
        {
            float tmp = GUI.HorizontalSlider(
                new Rect(25, 25 + 70 * i, 100, 50), this.model.factor[i], 0.0F, 1.0F);
            if (tmp != this.model.factor[i])
            {
                this.model.SetFactor(i, tmp);
            }

        }
    }
    #endregion

    #region View
    public void On_SetFactorSignal(int index, float initFactor, float curFactor)
    {   
        UpdateGeometryV2();
    }


    public Vector3 Sample(Vector3 start, Vector3 end, float factor)
    {
        return Vector3.Lerp(start, end, factor);
    }

    public Mesh mMesh;			  // First generated mesh
    public MeshFilter mFilter;    // Mesh filter for this draw call
    void UpdateGeometryV2()
    {
        if (mMesh == null) {
            mMesh = new Mesh();
            //mMesh.hideFlags = HideFlags.DontSave;
            mMesh.MarkDynamic();
        }
        

        //1   2 
        //0   3
        BetterList<Vector3> verts = new BetterList<Vector3>();
        BetterList<Vector2> uvs = new BetterList<Vector2>();
        BetterList<int> triangles = new BetterList<int>();

        for (int index = 0; index < this.pivot.Length; index++)
        {
            Transform curPivot = this.pivot[index];

            int nextIndex = (index >= (this.pivot.Length - 1) ? 0 : index + 1);
            Transform nextPivot = this.pivot[nextIndex]; ///下一个点

            Vector3 start = Sample(this.center.transform.localPosition, curPivot.transform.localPosition, this.model.factor[index]);
            Vector3 end = Sample(this.center.transform.localPosition, nextPivot.transform.localPosition, this.model.factor[nextIndex]);

            verts.Add(this.center.transform.localPosition);
            verts.Add(start);
            verts.Add(end);

            uvs.Add(new Vector2(0, 0));
            uvs.Add(new Vector2(0, 1));
            uvs.Add(new Vector2(1, 1));

            triangles.Add(3 * index);
            triangles.Add(3 * index + 1);
            triangles.Add(3 * index + 2);
        }

        mMesh.vertices = verts.ToArray();
        mMesh.uv = uvs.ToArray();
        mMesh.triangles = triangles.ToArray();
        //GenerateCachedIndexBuffer(verts.size, (verts.size >> 1) * 3);
        mMesh.RecalculateNormals();

        mFilter.mesh = mMesh;
    }
    #endregion
}
